package com.jiajia.study;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NioTest01 {
    public static void main(String[] args) throws IOException {
        String filePath = "C:\\IDEAWorkPlace\\2025_lab\\sunsuns_netty\\files\\test1.txt";
        // 1. 创建 channel : FileChannel
        FileChannel channel = new FileInputStream(filePath).getChannel();


        // 2. 创建缓冲区, 开辟 10 个字节的大小
        ByteBuffer buffer = ByteBuffer.allocate(10);

        // 3. 将通道的数据写入到缓冲区中,也就是从 channel 中读数据
        while(true){
            int read = channel.read(buffer);
            if(read == -1){
                break;
            }
            // 4. 切换读写模式
            buffer.flip();

            // 5. 从 buffer 中读数据
            while (buffer.hasRemaining()) {
                byte byteResult = buffer.get();
                System.out.println(" byte result is : " + (char)byteResult);
            }
            // 6. 设置 buffer 为写模式
            buffer.clear();
        }



    }
}
